// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/stmts/insert_stmt.h>
#include <cantor/transform/exprs/range_var.h>
#include <cantor/transform/clause/table_name.h>
#include <cantor/transform/exprs/row_expr.h>
#include <cantor/transform/dmls/assignment.h>
#include <cantor/transform/stmt.h>

#include "cantor/transform/clause/column_name.h"

namespace cantor::transform {

    void TransInsertStmt::resolve_ast(const cantor::parser::InsertStmt*node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        // priority
        insert_stmt.set_priority(node->priority);
        insert_stmt.set_is_replace(node->is_replace);
        insert_stmt.set_is_merge(node->is_merge);
        insert_stmt.set_is_ignore(node->is_ignore);
        /// table_name
        if (node->table_name) {
            status = TransTableName::resolve_to(node->table_name, insert_stmt.mutable_table_name());
            if (!status.ok()) {
                return;
            }
        }

        /// subquery_stmt
        if (node->subquery_stmt) {
            status = TransStmt::resolve_to(node->subquery_stmt, insert_stmt.mutable_subquery_stmt());
            if (!status.ok()) {
                return;
            }
        }
        /// columns
        auto cols = insert_stmt.mutable_columns();
        for (auto i =0; i <node->columns.size(); ++i) {
            status = TransColumnName::resolve_to(node->columns[i], cols->Add());
            if (!status.ok()) {
                return;
            }
        }

        /// lists
        auto lts = insert_stmt.mutable_lists();
        for (auto i =0; i <node->lists.size(); ++i) {
            status = TransRowExpr::resolve_to(node->lists[i], lts->Add());
            if (!status.ok()) {
                return;
            }
        }
        /// partition_names
        auto partition_names = insert_stmt.mutable_partition_names();
        for (auto i = 0; i <node->partition_names.size(); ++i) {
            partition_names->Add()->assign(node->partition_names[i].to_string());
        }
        /// on_duplicate
        auto ds = insert_stmt.mutable_on_duplicate();
        for (auto i =0; i <node->on_duplicate.size(); ++i) {
            status = TransAssignment::resolve_to(node->on_duplicate[i], ds->Add());
            if (!status.ok()) {
                return;
            }
        }

        set_proto_location(node, insert_stmt.mutable_location());
        set_proto_status(insert_stmt.mutable_status());
    }

    const turbo::Status &TransInsertStmt::check() {
        if (!status.ok()) {
            return status;
        }
        if (!insert_stmt.has_location()) {
            make_missing_required_field("location");
        }
        return status;
    }

    turbo::Status TransInsertStmt::resolve_to(turbo::Nonnull<const cantor::parser::InsertStmt*> arg, turbo::Nonnull<pb::ast::InsertStmt *>out) {
        TransInsertStmt trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.insert_stmt);
        return status;
    }
}  // namespace cantor::transform
